package com.mybook.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.my.ice.book.Message;
import com.my.ice.book.OnlineBook;
import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.Current;
import com.zeroc.Ice.ObjectAdapter;
import com.zeroc.Ice.Object;
import com.zeroc.Ice.Util;
import com.zeroc.IceBox.Service;

public class OnlineBookI implements OnlineBook, Service {
    private Logger logger = LoggerFactory.getLogger(OnlineBookI.class);
    private ObjectAdapter _adapter;
    private static final long seriaVersionUID = 1L;

    @Override
    public Message bookTick(Message msg, Current current) {
        logger.info("bookTick to call ." + logger.getClass().getName());
        //故意抛出异常打印堆栈
        if (msg.content.startsWith("ersror")) {
            throw new RuntimeException("bad call param");
        }
        logger.debug("bookTick called " + msg.content);
        msg.content = "xxxxxxxx";
        return msg;
    }

    @Override
    public void start(String name, Communicator communicator, String[] strings) {
        //创建objectAdapter, 这里和service同名
        _adapter = communicator.createObjectAdapter(name);
        //创建servant并激活
        Object object = this;
        _adapter.add(object, Util.stringToIdentity(name));
        _adapter.activate();
        logger.info(name + " started");

    }

    @Override
    public void stop() {
        logger.info(this._adapter.getName(), " stoped");
        //销毁adapter
        _adapter.destroy();
    }
}
